﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Serilog;

namespace SenderSerilog.Code
{
    public static class LogHelper
    {
        /// <summary>
        /// 项目编码
        /// </summary>
        private static string _projectCode { get; set; } = "MyProjectCode";

        /// <summary>
        /// 设置全局项目编码
        /// </summary>
        /// <param name="projectCode"></param>
        public static void SetGlobalProjectCode(string projectCode)
        {
            _projectCode = projectCode;
        }

        /// <summary>
        /// 冗余日志(0级), 不一定会被记录
        /// </summary>
        /// <param name="mes">日志消息</param>
        /// <param name="errorCode">日志编码</param>
        /// <param name="sender">发送者</param>
        /// <param name="version">版本</param>
        /// <param name="exceptionName">异常名</param>
        /// <returns></returns>
        public static string LogVerbose(string mes, string errorCode = "", string sender = "", string version = "", string exceptionName = "")
        {
            var input = GetLogInput(mes, errorCode, sender, version, exceptionName);
            Log.Verbose("{@input}", input);
            return input.ErrorCode;
        }

        /// <summary>
        /// 信息日志(1级)
        /// </summary>
        /// <param name="mes">日志消息</param>
        /// <param name="errorCode">日志编码</param>
        /// <param name="sender">发送者</param>
        /// <param name="version">版本</param>
        /// <param name="exceptionName">异常名</param>
        /// <returns></returns>
        public static string LogInformation(string mes, string errorCode = "", string sender = "", string version = "", string exceptionName = "")
        {
            var input = GetLogInput(mes, errorCode, sender, version, exceptionName);
            Log.Information("{@input}", input);
            return input.ErrorCode;
        }

        /// <summary>
        /// 调试日志(2级)
        /// </summary>
        /// <param name="mes">日志消息</param>
        /// <param name="errorCode">日志编码</param>
        /// <param name="sender">发送者</param>
        /// <param name="version">版本</param>
        /// <param name="exceptionName">异常名</param>
        /// <returns></returns>
        public static string LogDebug(string mes, string errorCode = "", string sender = "", string version = "", string exceptionName = "")
        {
            var input = GetLogInput(mes, errorCode, sender, version, exceptionName);
            Log.Debug("{@input}", input);
            return input.ErrorCode;
        }

        /// <summary>
        /// 警告日志(3级)
        /// </summary>
        /// <param name="mes">日志消息</param>
        /// <param name="errorCode">日志编码</param>
        /// <param name="sender">发送者</param>
        /// <param name="version">版本</param>
        /// <param name="exceptionName">异常名</param>
        /// <returns></returns>
        public static string LogWarning(string mes, string errorCode = "", string sender = "", string version = "", string exceptionName = "")
        {
            var input = GetLogInput(mes, errorCode, sender, version, exceptionName);
            Log.Warning("{@input}", input);
            return input.ErrorCode;
        }

        /// <summary>
        /// 异常日志(4级)
        /// </summary>
        /// <param name="mes">日志消息</param>
        /// <param name="errorCode">日志编码</param>
        /// <param name="sender">发送者</param>
        /// <param name="version">版本</param>
        /// <param name="exceptionName">异常名</param>
        /// <returns></returns>
        public static string LogError(string mes, string errorCode = "", string sender = "", string version = "", string exceptionName = "")
        {
            var input = GetLogInput(mes, errorCode, sender, version, exceptionName);
            Log.Error("{@input}", input);
            return input.ErrorCode;
        }

        /// <summary>
        /// 灾难日志(5级)
        /// </summary>
        /// <param name="mes">日志消息</param>
        /// <param name="errorCode">日志编码</param>
        /// <param name="sender">发送者</param>
        /// <param name="version">版本</param>
        /// <param name="exceptionName">异常名</param>
        /// <returns></returns>
        public static string LogFatal(string mes, string errorCode = "", string sender = "", string version = "", string exceptionName = "")
        {
            var input = GetLogInput(mes, errorCode, sender, version, exceptionName);
            Log.Fatal("{@input}", input);
            return input.ErrorCode;
        }

        private static LogInput GetLogInput(string mes, string errorCode = "", string sender = "", string version = "", string exceptionName = "")
        {
            return new LogInput()
            {
                ProjectCode = _projectCode,
                ErrorCode = string.IsNullOrEmpty(errorCode) ? Guid.NewGuid().ToString("N") : errorCode,
                ExceptionInfo = mes,
                ExceptionName = exceptionName,
                Sender = sender,
                Version = version,
                //如果需要添加客户端系统信息 请参考: https://blog.csdn.net/linxinfa/article/details/113257336
                //ClientSystem = 
            };
        }
    }
}
